CXX_TAG=gcc

VERSION=3.4.0
BASENAME="eigen-${VERSION}-${CXX_TAG}"

RM_SOURCE="false" # 是否删除源码
INSTALL="true"    # 是否编译及安装

SOURCE_DIR=${BASENAME}-source   # 源代码文件夹
BUILD_DIR=${BASENAME}-build     # build文件夹
INSTALL_DIR=${BASENAME}-install # 安装文件夹

if [ "${INSTALL}" == "true" ]; then

  EIGEN_PACKAGE_BASENAME=eigen-${VERSION}
  echo ${EIGEN_PACKAGE_BASENAME}.tar.gz

  if [ -f ${EIGEN_PACKAGE_BASENAME}.tar.gz ]; then
    echo "${EIGEN_PACKAGE_BASENAME}.tar.gz already existence"
  else
    echo "Retrieving eigen."
    wget "https://gitlab.com/libeigen/eigen/-/archive/${VERSION}/eigen-${VERSION}.tar.gz" || true

  fi

  rm -Rf ${SOURCE_DIR} 
  tar --no-same-owner -xvzf ${EIGEN_PACKAGE_BASENAME}.tar.gz
  mv ${EIGEN_PACKAGE_BASENAME} ${SOURCE_DIR}

  mkdir -p ${BUILD_DIR}
  pushd ${BUILD_DIR} >/dev/null
    cmake -G "Ninja" \
      -DCMAKE_CXX_FLAGS="-std=c++14" \
      -DCMAKE_INSTALL_PREFIX="../${INSTALL_DIR}" \
      ../${SOURCE_DIR}

    ninja
    ninja install

  popd >/dev/null

  if [ "${RM_SOURCE}" == "true" ]; then
    rm ${EIGEN_PACKAGE_BASENAME}.tar.gz
  fi

  rm -rf ${BUILD_DIR}

fi